/* 
    file:   ui_button.c
 */
/* ------------------------------------------------------------------------- */
#include "ui_imgbtn.h"
#include "ui_draw_img.h"

ui_style_t ui_imgbtn_def_1=
{
    .body=
    {
        .main_color = 0xb5b6,
        .grad_color = 0xc125,
        .radius = 0,
        .opa = 0xff,
    },
    .text=
    {
        0
    }
};

ui_style_t ui_imgbtn_def_2=
{
    .body=
    {
        .main_color = 0x5b2e,
        .grad_color = 0xc125,
        .radius = 0,
        .opa = 0xff,
    },
    .text=
    {
        0
    }
};

ui_style_t ui_imgbtn_def_3=
{
    .body=
    {
        .main_color = 0xc125,
        .grad_color = 0x5b2e,
        .radius = 0,
        .opa = 0xff,
    },
    .text=
    {
        0
    }
};

uint8_t ui_imgbtn_sc(struct ui_obj * obj, uint8_t sign, void * param)
{
    int sx = 0;
    int sy = 0;
    point realpoint;
    struct ui_core * pcore = NULL;

    ui_getlct(obj,&realpoint);
    sx = obj->area.x2-obj->area.x1;
    sy = obj->area.y2-obj->area.y1;

    if(param)
    {
        pcore = (struct ui_core *)param;
        if((sign == ui_signal_init_r)||(sign == ui_signal_releassed))
        {
            ui_draw_img_a(pcore->pscr,obj,1,obj->style_p->body.main_color);
        }
        if((sign == ui_signal_pressed)||(sign == ui_signal_pressing))
        {
            ui_draw_img_a(pcore->pscr,obj,1,obj->style_p->body.grad_color);
        }
    }
    
    return 0;
}

struct ui_obj * ui_newimgbtn(struct ui_obj * ppar)
{
    struct ui_obj * pnewobj = NULL;
    pnewobj = ui_newobj(ppar);

    pnewobj->signal_cb = ui_imgbtn_sc;
    pnewobj->style_p = &ui_imgbtn_def_1;

    pnewobj->objdat = tlsf_malloc(sizeof(void *));
    *(void **)(pnewobj->objdat) = NULL;

    pnewobj->touch = 1;

    return pnewobj;
}
void ui_imgbtn_set_pos(struct ui_obj * pobj,uint32_t px,uint32_t py)
{
    uint32_t sx,sy;
    sx = pobj->area.x2-pobj->area.x1;
    sy = pobj->area.y2-pobj->area.y1;
    pobj->area.x1 = px;
    pobj->area.y1 = py;
    pobj->area.x2 = px + sx;
    pobj->area.y2 = py + sy;
}

void ui_imgbtn_set_img(struct ui_obj * pobj,const void * pimg)
{
    lv_img_dsc_t * pimgt;
    *(void **)(pobj->objdat) = (void *)pimg;
    pimgt = (lv_img_dsc_t *)pimg;
    pobj->area.x2 = pobj->area.x1 + pimgt->header.w;
    pobj->area.y2 = pobj->area.y1 + pimgt->header.h;
}
void ui_imgbtn_set_style(struct ui_obj * pobj,ui_style_t * pstyle)
{
    pobj->style_p = pstyle;
}
